<% layout('/layouts/default.html', {title: '用户管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-people"></i> ${text(empUser.isNewRecord ? '新增用户' : op == 'auth' ? '用户分配角色' : '编辑用户')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${empUser}" action="${ctx}/vip/agent/save" method="post"  class="form-horizontal">
			<#form:hidden name="op" value="${op}"/>
			<#form:hidden name="userType" value="employee"/>
			<#form:hidden path="userCode"/>
			<div class="box-body">
				<% if(op=='auth'){ %><br/><% } %>
				<% if(op == 'add' || op == 'edit') { %>
				<div class="row">
					<#form:hidden name="employee.office.officeCode" value="SDQD1"/>
					<#form:hidden name="employee.office.officeName" value="会员系统"/>
					<#form:hidden name="employee.company.companyCode" value="SDQD1"/>
					<#form:hidden name="employee.company.companyName" value="会员系统"/>
					<#form:hidden name="userName" id="userName" value="${empUser.userName}"/>
					<input type="hidden" name="employee.employeePosts" id="employee-employeePosts">
				</div>
				<% } %>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('登录账号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:hidden name="oldLoginCode" value="${empUser.loginCode}"/>
								<#form:input path="loginCode" minlength="4" maxlength="20" readonly="${op=='auth'}"
										class="form-control required userName"
										remote="${ctx}/sys/user/checkLoginCode?oldLoginCode=${empUser.loginCode}"
										data-msg-remote="${text('登录账号已存在')}"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('姓名')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="employee.empName" maxlength="32" class="form-control "/>
							</div>
						</div>
					</div>
				</div>
				<% if(op == 'add' || op == 'edit') { %>
				<div class="row">
					<!--<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('电子邮箱')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<div class="input-group">
									<#form:input path="email" maxlength="300" class="form-control email"/>
									<span class="input-group-addon"><i class="fa fa-fw fa-envelope" style="margin-top:-2px;display:block;"></i></span>
								</div>
							</div>
						</div>
					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('手机号码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<div class="input-group">
									<#form:input path="mobile" maxlength="100" class="form-control mobile"/>
									<span class="input-group-addon"><i class="fa fa-fw fa-mobile" style="margin-top:-2px;display:block;"></i></span>
								</div>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('所属项目')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select multiple="true" path="employee.agentProjects" items="${projectList}"
								itemLabel="name" itemValue="id" class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
				<!--<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('办公电话')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<div class="input-group">
									<#form:input path="phone" maxlength="100" class="form-control phone"/>
									<span class="input-group-addon"><i class="fa fa-fw fa-phone"></i></span>
								</div>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('所属项目')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select multiple="true" path="employee.agentProjects" items="${projectList}"
								itemLabel="name" itemValue="id" class="form-control"/>
							</div>
						</div>
					</div>
				</div>-->
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control "/>
							</div>
						</div>
					</div>
				</div>
				<% } %>
				<div class="form-unit">${text('分配角色')}</div>
				<div class="pl10 pb20">
					<table id="roleGrid"></table>
					<#form:hidden name="userRoleString"/>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('vip:agent:edit')){ %>
						<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
    function fromCheck() {
        //判断是否多选了角色，此页面不允许多选角色
        if($("#userRoleString").val().indexOf(',') != -1){
            js.showErrorMessage("此页面不能多选角色");
            return false;
        }
        if($("#userRoleString").val() == ''){
            js.showErrorMessage("请选择角色");
            return false;
        }
        return true;
    }

    //设置用户岗位
    function setPost(rodeCode) {
		if(rodeCode == '1000')
            return  '4';
        if(rodeCode == '1001')
            return  '1';
        if(rodeCode == '1003')
            return  '2';
        if(rodeCode == '1004')
            return  '3';
        if(rodeCode == '1005')
            return  '5';
        if(rodeCode == '1006')
            return '6';
        if(rodeCode == '1007')
            return '7';
    }
    $("#inputForm").validate({
        submitHandler: function(form){
			// 获取选中角色
			$("#userRoleString").val(roleGrid.dataGrid('getSelectRows').join(','));
			$('#employee-employeePosts').val(setPost($("#userRoleString").val()));//设置用户岗位
            $('#userName').val($('#employee_empName').val());
			if(fromCheck()){
                js.ajaxSubmitForm($(form), function(data){
                    js.showMessage(data.message);
                    if(data.result == Global.TRUE){
                        js.closeCurrentTabPage(function(contentWindow){
                            contentWindow.page();
                        });
                    }
                }, "json");
			}
        }
    });

// 加载角色列表
        var roleGrid = $("#roleGrid").dataGrid({
            url: '${ctx}/sys/role/treeData',
            postData: [
                {name:'userType',value:'employee'},
                {name:'ctrlPermi',value:'2'}
            ],
            columnModel: [
                {header:'${text('角色名称')}', name:'name', sortable:false, width:100, align:"center"},
                {header:'${text('角色编码')}', name:'id', sortable:false, width:100, align:"center"}
            ],
            showCheckbox: true,
            autoGridHeight: function(){
                return 'auto';
            },
            autoGridWidth: function(){
                return $('#inputForm .box-body').width()-20;
            },
            ajaxSuccess: function(){
            <% for (role in roleList!){ %>
                    roleGrid.dataGrid('setSelectRow', '${role.roleCode}');
                <% } %>
            }
        });
</script>
